package com.ruoyi.web.controller.mobile;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ValueIsNullUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.info.domain.vo.VinfoCountDaily;
import com.ruoyi.info.service.IVinfoCountOrderService;
import com.ruoyi.mobile.domain.*;
import com.ruoyi.mobile.service.IMobileCarInfoStatistics;
import com.ruoyi.mobile.service.IMobileDataStatisticsService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * @Author Jack_Lee
 * @Date 2024/7/16
 * 移动端登录
 */
@RestController
@RequestMapping("/mobile")
public class MobileController {

    @Resource
    private IMobileCarInfoStatistics carInfoStatistics;

    @Resource
    private IMobileDataStatisticsService dataStatisticsService;



    @GetMapping("/MonthStatistics")
    public AjaxResult MonthStatisticsInfo(@RequestBody Map<String, Object> paramMap) {
        // 如果map 为空  并且里面的值为空字符串 则直接提示
        if (ValueIsNullUtils.isEmptyOrContainsEmptyValue(paramMap)) {
            return AjaxResult.error("数据有误!");
        }
        List<MobileMonthStatisticsVo> mobileMonthStatisticsVos = carInfoStatistics.MonthStatistics(paramMap);
        if (ObjectUtils.isEmpty(mobileMonthStatisticsVos)) {
            return AjaxResult.error("本公司当月没有相关车辆信息");
        }
        return AjaxResult.success("查询成功", mobileMonthStatisticsVos);
    }

    /**
     * 日数据统计 按公司
     * @param deptId
     * @return
     */
    @GetMapping("/DailyDataStatistics/{deptId}")
    public AjaxResult CarInfoDailyStatistics(@PathVariable("deptId") Long deptId) {

        if (ObjectUtil.isNull(deptId)) {
            return AjaxResult.error("数据有误!");
        }
        List<MobileDailyDataVo> monthStatisticsVos = dataStatisticsService.dailyDataStatistics(deptId);

        return AjaxResult.success("查询成功", monthStatisticsVos);
    }

    /**
     * 月数据统计  按公司
     * @param dataMap
     * @return
     */
    @GetMapping("/MonthDataStatistics")
    public AjaxResult CarInfoMonthStatistics(@RequestParam Map<String, Object> dataMap) {
        // 如果map 为空  并且里面的值为空字符串 则直接提示
        if (ValueIsNullUtils.isEmptyOrContainsEmptyValue(dataMap)) {
            return AjaxResult.error("数据有误!");
        }
        List<MobileMonthDataVo> monthStatisticsVos = dataStatisticsService.MonthDataStatistics(dataMap);
        System.out.println(monthStatisticsVos);

        return AjaxResult.success("查询成功", monthStatisticsVos);
    }


    /**
     * 月数据统计  按公司
     * @param dateMap
     * @return
     */
    @GetMapping("/MonthDataCount")
    public AjaxResult CarInfoMonthCount(@RequestParam Map<String, Object> dateMap) {
        // 如果map 为空  并且里面的值为空字符串 则直接提示
        if (ValueIsNullUtils.isEmptyOrContainsEmptyValue(dateMap)) {
            return AjaxResult.error("数据有误!");
        }
        List<MonthDataCountVo> monthDataCountVos = dataStatisticsService.MonthDataCount(dateMap);
        Double totalWeight = 0.00;
        Integer totalRecordCount = 0;
        for (MonthDataCountVo monthDataCountVo : monthDataCountVos) {
            totalRecordCount += monthDataCountVo.getRecordCount();
            totalWeight += monthDataCountVo.getTotalWeight();
        }

        HashMap<Object, Object> resultMap = new HashMap<>();
        resultMap.put("dataList", monthDataCountVos);
        resultMap.put("totalWeight", totalWeight);
        resultMap.put("totalRecordCount", totalRecordCount);

        System.out.println(monthDataCountVos);
        return AjaxResult.success("查询成功", resultMap);
    }

    @GetMapping("/DailyDataCount")
    public AjaxResult CarInfoDailyCount(@RequestParam Map<String, Object> dateMap) {
        // 如果map 为空  并且里面的值为空字符串 则直接提示
        if (ValueIsNullUtils.isEmptyOrContainsEmptyValue(dateMap)) {
            return AjaxResult.error("数据有误!");
        }
        List<DailyDataCountVo> dailyDataCountVos = dataStatisticsService.DailyDataCount(dateMap);
        System.out.println(dailyDataCountVos);
        return AjaxResult.success("查询成功", dailyDataCountVos);
    }


    @GetMapping("/exportDaily/{deptId}")
    public void exportDaily(@PathVariable("deptId") Long deptId,
                            HttpServletResponse response) {
        System.out.println("deptId = " + deptId);
        List<MobileDailyDataVo> list = dataStatisticsService.dailyDataStatistics(deptId);
        System.out.println("list = " + list);
        ExcelUtil<MobileDailyDataVo> util = new ExcelUtil<>(MobileDailyDataVo.class);
        util.exportExcel(response, list, "导出日数据数据");

    }


}
